package cn.swust.designpatternlearningplatform.controller;

import cn.swust.designpatternlearningplatform.common.AnnotationUtils.Authentication;
import cn.swust.designpatternlearningplatform.common.Result.PageInfo;
import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.common.utils.JwtUtils;
import cn.swust.designpatternlearningplatform.domain.bo.StudentTopicTypeBo;
import cn.swust.designpatternlearningplatform.domain.dto.TopicShieldDto;
import cn.swust.designpatternlearningplatform.domain.enums.TopicTypeEnum;
import cn.swust.designpatternlearningplatform.domain.enums.TopicUpdateTypeEnum;
import cn.swust.designpatternlearningplatform.domain.vo.TopicShieldVo;
import cn.swust.designpatternlearningplatform.service.TopicShieldService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;


@Slf4j
@RestController
@RequestMapping("/topic")
public class TopicShiedController {

    /**
     * 题目屏蔽
     *
     * @author Tang
     * @date 2023/5/2 22:05
     **/
    @Resource
    private TopicShieldService topicShieldService;

    /**
     * 学生增加屏蔽题
     *
     * @return cn.swust.designpatternlearningplatform.common.Result.Result<java.lang.String>
     * @author Tang
     * @date 2023/4/16 1:19
     **/
    @Authentication
    @PostMapping("/shield")
    public R<String> shield(@RequestBody TopicShieldVo topicShieldVo) {
        String number = AuthUserContext.get().getNumber();
        topicShieldService.addTopicShield(number, topicShieldVo.getTopicIds());
        return R.success("屏蔽成功");
    }

    /**
     * 取消屏蔽
     *
     * @param token
     * @param topicId
     * @return cn.swust.designpatternlearningplatform.common.Result.R<java.lang.String>
     * @author Tang
     * @date 2023/4/17 20:39
     **/
    @Authentication
    @DeleteMapping("/shield")
    public R<String> cancelShield(@RequestHeader("token") String token, @RequestParam("topicId") Integer topicId) {
        log.info("{}", topicId);
        String number = (String) JwtUtils.getClaims(token).get("number");
        StudentTopicTypeBo studentTopicTypeBo = new StudentTopicTypeBo();
        studentTopicTypeBo.setTopicId(Collections.singletonList(topicId));
        studentTopicTypeBo.setStudentNumber(number);
        studentTopicTypeBo.setTopicType(TopicUpdateTypeEnum.SHIELD.getTypeName());
        topicShieldService.cancelTopicShield(studentTopicTypeBo);
        return R.success("取消屏蔽");
    }

    /**
     * 分页查询屏蔽题
     *
     * @return cn.swust.designpatternlearningplatform.common.Result.R<cn.swust.designpatternlearningplatform.common.Result.PageInfo < cn.swust.designpatternlearningplatform.domain.dto.TopicShieldDto>>
     * @author Tang
     * @date 2023/4/19 21:34
     **/
    @Authentication
    @GetMapping("/shield/list")
    public R<PageInfo<TopicShieldDto>> getShieldList(@RequestParam("currentPage") Integer current,
                                                     @RequestParam("pageSize") Integer size,
                                                     @RequestParam(value = "topicType", required = false) Integer topicType) {
        if (current <= 0) {
            return R.success("页码错误");
        }
        //获取学生的学号
        String studentNumber = AuthUserContext.get().getNumber();
        StudentTopicTypeBo studentTopicTypeBo = new StudentTopicTypeBo();
        studentTopicTypeBo.setStudentNumber(studentNumber);
        studentTopicTypeBo.setCurrent(current);
        studentTopicTypeBo.setSize(size);
        studentTopicTypeBo.setUpdateType(TopicUpdateTypeEnum.SHIELD.getTypeName());
        studentTopicTypeBo.setTopicType(TopicTypeEnum.topicType(topicType));
        //分页查询屏蔽题目
        PageInfo<TopicShieldDto> topicShieldList = topicShieldService.getTopicShieldList(studentTopicTypeBo);
        return R.success(topicShieldList);
    }
}
